<?php
namespace App\Services\Admin;

use App\Services\CommonService;
use App\Model\User;
use App\Model\Integra;
use App\Model\Exchangelog;
use App\Model\Recipient;
use App\Model\Exchangegoods;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;

use Illuminate\Foundation\Http\FormRequest;

class UserService extends CommonService
{
    /**
     * 用户信息查询
     *@param array  数据
     *@param array  验证规则
     */

    public function UserOne($data,$rules,$errmessge){
        $validatorres=$this->Validator($data,$rules,$errmessge);
        if ($validatorres==='success')
        {
            $arr = User::all()->where('id', $data['id'])->toArray();
            return ['status'=>'success','data'=>$arr];
        }
        else
        {
            return ['status'=>'success','data'=>$validatorres];
        }
    }

    /**
     * 查询用户积分
     * @param $id
     * @return mixed
     */
    public function Userintegral($id)
    {
        $arr = User::all()->where('id', $id)->toArray();
        return $arr['u_integral'];
    }

    /**
     * 用户信息修改
     *@param array  数据
     *@param array  验证规则
     */
    public function ServiceSave($data,$rules,$errmessge){
        $validatorres=$this->Validator($data,$rules,$errmessge);
        if ($validatorres==='success')
        {
            $upres=User::where('id',$data['id'])->update($data);
            return ['status'=>'success','data'=>$upres];
        }
        else
        {
            return ['status'=>'error','data'=>$validatorres];
        }
    }

    /**修改密码
     * @param $data 验证的数据
     * @param $rules 验证规则
     * @param $errmessge  自定义错误信息
     * @return array   返回信息
     */
    public function uppwd($data,$rules,$errmessge)
    {
        $validatorres=$this->Validator($data,$rules,$errmessge);
        if ($validatorres==='success')
        {
            $upres=User::where('id',$data['id'])->update(['u_password'=>$data['password']]);
            return ['status'=>'success','data'=>$upres];
        }
        else
        {
            return ['status'=>'error','data'=>$validatorres];
        }
    }

    /**
     * 查询用户积分 商品积分
     * @param $data
     * @param $rules
     * @param $errmessge
     * @return array
     */
    public function GoodslistUserintegral($data,$rules,$errmessge)
    {
        $validatorres=$this->Validator($data,$rules,$errmessge);
//        如果用户信息有效 就查询商品表  用户信息表
        if ($validatorres==='success')
        {
            $userintegral=$this->useringralsum($data['id']);
            $goodspage = DB::table('re_exchange')->paginate(1);
            $datas=['userintegral'=>$userintegral,'goodspage'=>$goodspage];
            return ['status'=>'success','data'=>$datas];
        }
        else
        {
            return ['status'=>'error','data'=>$validatorres];
        }
    }


    /**
     * 查询用户积分和积分记录日志
     *return array
     */
    public function UserIntegralog($data,$rules,$errmessge)
    {
        $validatorres=$this->Validator($data,$rules,$errmessge);
        if ($validatorres==='success')
        {
            $userintegral=$this->useringralsum($data['id']);
            $integralog=Integra::all()->where('integra_uid',$data['id'])->toArray();
            return ['status'=>'success','data'=>['integrasum'=>$userintegral,'integralog'=>$integralog]];
        }
        else
        {
            return ['status'=>'error','data'=>$validatorres];
        }
    }

    /**
     * 兑换商品记录日志
     *@param int  用户ID
     *return array 日志信息
     */
    public function egoodslog($data,$rules,$errmessge)
    {
        //        先验证用户id 没错误  查询商品兑换记录
        $validatorres=$this->Validator($data,$rules,$errmessge);
        if ($validatorres==='success')
        {
            $exchangegoods=$this->Exchangegoodslog($data['id']);
           return ['data'=>$exchangegoods];
           
             
        }
        else
        {
            return ['status'=>'error','data'=>$validatorres];
        }

    }

    /**
     * 积分兑换商品
     * @param $gid
     * @param $uid
     * @return array|string
     */
    public function integralexchangegood($gid,$uid,$num,$addr_name,$addr_phone,$addr_address,$addr_detail)
    {
//        查看商品积分 和用户积分  判断积分是否足够
        $gooddetails=$this->goodone($gid);
        $goodintegral=$gooddetails[0]['exchange_score'];  //商品所需积分
        $exchange_name=$gooddetails[0]['exchange_name'];  //商品名称
        $goodsnum=$gooddetails[0]['exchange_num'];  //商品总数
        if($goodsnum<1)
        {
            return  ['status'=>'error','data'=>'商品库存不足'];
        }
        $usersuminte=$this->useringralsum($uid);         //用户总积分
        $goodssumintegrals=$goodintegral*$num;
        if($goodssumintegrals>$usersuminte)
        {
            return ['status'=>'error','data'=>'用户积分不足'];
        }
        DB::beginTransaction();
                     $addr_recipient=DB::table('re_recipient')->insertGetId([
                'recipient_name' => $addr_name,
                'recipient_tel' => $addr_phone,
                'recipient_add' => $addr_address,
                'recipient_address' => $addr_detail,  //收货详细地址
            ]);
        $uintegral = DB::table('re_user')->where('id', $uid)->decrement('u_integral', $goodssumintegrals);
        $goodminus = DB::table('re_exchange')->where('exchange_id', 1)->decrement('exchange_num', 1);
         // 兑换商品日志添加一条信息
             $exchange_log_res = DB::table('re_exchange_log')->insertGetId([
                'goods_id' => $gid,
                'exchangere_uid' => $uid,
                'exchangere_num' => 1,
                'exchangere_status' => 1,  
            ]);
             // 兑换记录积分日志
             $integra_log_res = DB::table('re_integra_log')->insertGetId([
                'integra_uid' => $uid,
                'integra_scord' => '-'.$goodssumintegrals,
                'integra_informat' => '兑换商品：'.$exchange_name,
                'integra_time' => time(),  //默认待出货
            ]);
        if ($uintegral && $goodminus && $exchange_log_res && $integra_log_res && $addr_recipient) {
            //如果语句执行成功就进行提交
            DB::commit();
            return ['status'=>'success'];
        } else {
            DB::rollback();
            return ['status'=>'error','data'=>'修改失败'];
        }
    }

//                                             以下函数类内调用

    /**
     * 验证数据是否正确
     * @param $data
     * @param $rules
     * @param $errmessge
     * @return string
     */
    public function Validator($data,$rules,$errmessge)
    {
        $validate = Validator::make($data, $rules,$errmessge);
        //返回错误信息
        if($validate->fails()) {
            return $validate->errors()->all();
        }else{
            return 'success';
        }
    }

    /**
     * 用户兑换商品记录
     * @param $id
     * @return array
     */
    public function  Exchangegoodslog($uid)
    {
        return   $data=DB::select('SELECT * from  re_exchange_log as l INNER  JOIN re_exchange as g  ON l.goods_id=g.exchange_id  WHERE  exchangere_uid='.$uid);
    }

    /**
     * 用户总积分查询
     * @param $id
     * @return mixed
     */
    public function useringralsum($id)
    {
        $arr = User::all()->where('id', $id)->toArray();
        return $arr[0]['u_integral'];
    }

    /**
     * 查询所有商品
     * @param $p
     * @return mixed
     */
    public function goodsall($p)
    {
        if (!isset($p)) {
            $p=1;
        }
        $limit=2*($p-1);
        return Exchangegoods::all();
    }

    /**
     * 返回单条商品详情
     * @param $gid
     * @return mixed
     */
    public function goodone($gid)
    {
        return Exchangegoods::all()->where('exchange_id', $gid)->toArray();
    }


}